/**
 * Copyright 2007-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.asemantics.anthill.chatconsole;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPException;

/**
 * Exposes a chat as a console providing output methods.
 */
public class ChatConsoleOutputStream {

    /**
     * The underlying chat.
     */
    private Chat chat;

    /**
     * The internal string buffer.
     */
    private final StringBuffer buffer;

    /**
     * If <code>true</code> there is no buffering, all is written on the
     * console is sent to the chat.
     */
    private boolean autoflush;

    /**
     * Constructor.
     *
     * @param c the cha to wrap with this out stream.
     */
    public ChatConsoleOutputStream(Chat c) {
        chat = c;
        buffer = new StringBuffer();
    }

    /**
     * Returns the autoflush value.
     *
     * @return
     */
    public boolean isAutoflush() {
        return autoflush;
    }

    /**
     * Sets the autoflush value.
     *
     * @param autoflush
     */
    public void setAutoflush(boolean autoflush) throws XMPPException {
        this.autoflush = autoflush;
        if(autoflush) {
            flush();
        }
    }

    /**
     * Constructor.
     */
    public ChatConsoleOutputStream() {
        this(null);
    }

    /**
     * Goes to new line.
     */
    public void newline() {
        buffer.append("\n");
    }

    /**
     * Prints a message on the console output.
     *
     * @param msg
     */
    public void print(String msg) {
        buffer.append(msg);
    }

    /**
     * Prints a message on the console output and goes to
     * new line.
     *
     * @param msg
     */
    public void println(String msg) {
        print(msg);
        newline();
    }

    /**
     * Writes directly on the chat.
     * 
     * @param msg
     */
    public void flushprint(String msg) throws XMPPException {
        if(chat == null) {
            throw new IllegalArgumentException();
        }

        chat.sendMessage(msg);
    }

    /**
     * Flushes the content of the buffer into the current chat.
     */
    public void flush() throws XMPPException {
        if(chat != null) {
            try {
                chat.sendMessage(buffer.toString());
            } finally {
                reset();
            }
        } else {
            throw new IllegalStateException("A chat must be setted first.");
        }
    }

    /**
     * Returns the message body.
     *
     * @return
     */
    protected String getMessageBody() {
        return buffer.toString();
    }

    /**
     * Resets the console chat buffer.
     */
    protected void reset() {
        buffer.delete(0, buffer.length());
    }

    /**
     * Sets a new underlying chat, flushing first on the current one if any.
     * @param c
     */
    protected void setChat(Chat c) throws XMPPException {
        if(chat != null) {
            flush();
        }
        chat = c;
    }

}
